//
//  AXMySrdzWdJsVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/11/8.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "AXMySrdzWdJsVC.h"
#import "UIView+SDAutoLayout.h"
#import "ZXCategories.h"
#import "Header.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "AXMySrdzWdJsCell.h"
#import "AXMySrdzWdJsModel.h"
#import "UIViewController+HBD.h"
#import "AXMySrdzWdjsSchemeDetailsVC.h"
#import "AXMySrdzWdJsModel.h"




@interface AXMySrdzWdJsVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) NSMutableArray<AXMySrdzWdJsModel *>  *modelsArray;
@property (nonatomic , strong) UITableView *tableView;

@property (nonatomic , assign) NSInteger pagSize;
@property (nonatomic , assign) NSInteger pagNum;
@property (nonatomic , assign) Boolean hasPageNext;
@property (nonatomic , strong) NSString *userID;
@property (nonatomic , assign) Boolean pageFlag;


@end

@implementation AXMySrdzWdJsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    [self setlayout];
    [self setupNavigationItems];
    self.hbd_barTintColor = [UIColor whiteColor];
    self.hbd_barShadowHidden = YES;
}

- (void)creatView {
    self.pagSize=10;
    self.pagNum=1;
    self.hasPageNext=true;
    self.pageFlag=false;
    self.userID=@"zzh001";
    
    self.tableView = [[UITableView alloc]init];
    /** 去掉cell线 */
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[AXMySrdzWdJsCell class] forCellReuseIdentifier:[AXMySrdzWdJsCell reuseID]];
    [self.view addSubview:self.tableView];
    
    /**设置刷新*/
    self.tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestStrategyList];
        self.pageFlag=true;
    }];
    
    self.tableView.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if(self.hasPageNext){
            [self requestStrategyList];
            self.pageFlag=false;
        }else{
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
    }];
    
    LKEmptyViewConfig *configView=[LKEmptyViewConfig setupEmptyViewConfigForTableView:self.tableView];
    configView.message=@"暂无数据";
    
    [self.tableView.mj_header beginRefreshing];
    [self requestStrategyList];
    
}


- (void)setlayout {
    
    
    self.tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, NAVIGATION_HEIGHT)
    .widthIs(Wi)
    .heightIs(TableViewHeight);
    
    
}

- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 30, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    /** title */
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text = @"我的技术";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT_EIGHTEEN;
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    self.navigationItem.titleView = titleLabel;
    
    
}

#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)search:(UIButton *)btn {
    
}



#pragma mark - tableViewDelegate & tableViewDatasorce



//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelsArray.count;
}

/** cell的高度 */

/** cell 自适应第二步 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return [tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model" cellClass:[AXMySrdzWdJsCell class] contentViewWidth:SCREEN_WIDTH];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AXMySrdzWdJsCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXMySrdzWdJsCell reuseID] forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    /** 查看更多的按钮方法 */
    cell.tapCelllickedBlock = ^(NSInteger index) {
        AXMySrdzWdjsSchemeDetailsVC *vc = [[AXMySrdzWdjsSchemeDetailsVC alloc]init];
        AXMySrdzWdJsModel *srdzWdJs=self.modelsArray[index];
        vc.strategyId= srdzWdJs.fStrategyId;
        [self.navigationController pushViewController:vc animated:YES];
        
    };
    
    //判断距离
    if (indexPath.row == 0) {
        
        cell.bgView.sd_layout
        .leftSpaceToView(cell.contentView, 10)
        .topSpaceToView(cell.contentView, 10)
        .widthIs(SCREEN_WIDTH - 20)
        .autoHeightRatio(0)
        .minHeightIs(131);
        
    }
    else {
        
        cell.bgView.sd_layout
        .leftSpaceToView(cell.contentView, 10)
        .topSpaceToView(cell.contentView, 5)
        .widthIs(SCREEN_WIDTH - 20)
        .autoHeightRatio(0)
        .minHeightIs(131);
        
    }
    cell.model = self.modelsArray[indexPath.row];
    return cell;
    
    
}

/*** 获取我的技术列表*/
-(void) requestStrategyList{
    
    ZX_WEAK_SELF
    
    NSMutableDictionary *param=[NSMutableDictionary dictionary];
    param[@"fUserId"]=self.userID;
    param[@"pageNum"]=@(self.pagNum);
    param[@"pageSize"]=@(self.pagSize);
    
    [NetWorkHelp yl_postDataWith:param url:URL_SPLIT(URL_AgricultureCircleProfessorDiagnose,URL_AgricultureCircleProfessorDiagnose_strategyList) success:^(id respose) {
        if([respose[@"header"][@"statusCode"] integerValue]==200){
            self.hasPageNext=[respose [@"body"][@"data"][@"hasNextPage"] boolValue];
            NSMutableArray<AXMySrdzWdJsModel *> *models = [NSMutableArray<AXMySrdzWdJsModel *> array];
            models=[AXMySrdzWdJsModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"data"][@"list"]];
            if(self.pageFlag){
                [weakSelf.modelsArray removeAllObjects];
            }
            [weakSelf.modelsArray addObjectsFromArray:models];
            [self.tableView reloadData];
        }
        if(self.hasPageNext){
            self.pagSize++;
        }
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    }];
    
}


- (NSMutableArray<AXMySrdzWdJsModel *> *)modelsArray {
    
    if (!_modelsArray) {
        
        _modelsArray = [NSMutableArray<AXMySrdzWdJsModel *> array];
    }
    return _modelsArray;
    
}



@end


